package 分类.一维动规;

/**
 * 链接：https://www.nowcoder.com/practice/6d29638c85bb4ffd80c020fe244baf11?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj
 * 二维dp
 */
public class 最长公共子序列 {
    public static int LCS (String s1, String s2) {
        // write code here
        if(s1.length()==0||s2.length()==0){
            return -1;
        }
        int m=s1.length();
        int n=s2.length();
        int[][] dp=new int[m+1][n+1];

        for (int i=1;i<=m;i++){
            for (int j=1;j<=n;j++){
                if(i==0||j==0){
                    dp[i][j]=0;
                    continue;
                }else {
                    if(s1.charAt(i-1)==s2.charAt(j-1)){
                        dp[i][j]=1+dp[i-1][j-1];
                    }else {
                        dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
                    }
                }
            }
        }
        return dp[m][n];
    }

    public static void main(String[] args) {
        String a="1A2C3D4B56",b="B1D23A456A";
        System.out.println(LCS(a, b));
    }
}
